/**
 * 
 */
package com.game.jumpdino.logic.visual;

import java.util.ArrayList;
import java.util.List;

import com.game.jumpdino.R;
import com.game.jumpdino.JumpingView;
import com.game.jumpdino.logic.Playground;
import com.game.jumpdino.logic.VisualTurnedListener;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;

/**
 * @author HugoGu
 *
 */
public class Avatar extends VisualBase {
	private Drawable altetiveImage;
	private Drawable imageS;
	private Drawable altetiveImageS;
	private boolean readyForSwitch = true;
	private AnimationDrawable dizy;
	private int jumping = -1;
	private boolean turned = false;
	private List<VisualTurnedListener> turnListeners;
	private boolean hasTool = false;
	private float jumpSpeed;
	public static final float defaultG = 1.5f;
	public static final float defaultGX = 0.8f;
	
	public Avatar(){
		turnListeners = new ArrayList<VisualTurnedListener>();
		setG(defaultG);
		maxVx = 14;
		gx = defaultGX;
		bonusOnEdge = false;
	}
	
	public boolean isHasTool() {
		return hasTool;
	}

	public void setHasTool(boolean hastool) {
		this.hasTool = hastool;
	}
	
	public Drawable setAltetiveImage(Drawable alImage){
		Drawable previous = this.altetiveImage;
		if (image != null){
			this.altetiveImage = alImage;
		}
		
		return previous;
	}
	
	public boolean getTurned(){
		return turned;
	}
	
	public void setTurned(boolean turned){
		if (turned != this.turned){
			this.turned = turned;
			for (VisualTurnedListener listener : turnListeners){
				listener.onTurned(this);
			}
		}
	}
	
	/**
	 * @return the turned
	 */
	public void removeTurnedListener(VisualTurnedListener listener) {
		turnListeners.remove(listener);
	}

	/**
	 * @param turned the turned to set
	 */
	public void addTurnedListener(VisualTurnedListener listener) {
		turnListeners.add(listener);
	}
	
	@Override
	public void onInitialized(Context context){
		super.onInitialized(context);
		imageS = context.getResources().getDrawable(R.drawable.pandas);
		altetiveImageS = context.getResources().getDrawable(R.drawable.panda2s);
		altetiveImage = context.getResources().getDrawable(R.drawable.panda2);
		dizy = (AnimationDrawable)context.getResources().getDrawable(R.drawable.dizy);
	}
	
	@Override
	public void setVy(float y){ 
		// While jumping
		if (this.getVelocity().y > 0 && y < 0 && this.getAttachTo() == null){
			jumping = 1;
			jumpSpeed = y;
			switchShort();
			setG(0);
			super.setVy(0);
		} else {
			super.setVy(y);
		}
	}
	
	@Override
	public void drawOn(Canvas c){
		super.drawOn(c);
		if (jumping == 0){
			switchShort();
			switchImage();
			jumping = -1;
			setG(defaultG);
			super.setVy(jumpSpeed);
		} else if (jumping > 0) { 
			--jumping;
		}
		
		if (Playground.getCurrent().getReverseOperation() < 0){
		    int x = this.getPosition().left + 20;
		    int y = this.getPosition().top - 35;
		    //if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
		    	dizy.setBounds(new Rect(x, y, x + dizy.getIntrinsicWidth(), y + dizy.getIntrinsicHeight()));
		    //} else {
		    //	dizy.setBounds(new Rect(x, y, x + dizy.getIntrinsicHeight(), y + dizy.getIntrinsicWidth()));
		    //}
			
			dizy.setCallback(JumpingView.getCurrent());
			dizy.draw(c);
			dizy.start();
		}
	}
	
	private void switchShort(){
		readyForSwitch = !readyForSwitch;
		
		Drawable temp = image;
		image = imageS;
		imageS = temp;
	}
	
	private void switchImage(){
		if (!readyForSwitch){
			switchShort();
		}
		
		this.setTurned(!turned);			
		Drawable temp = image;
		image = altetiveImage;
		altetiveImage = temp;
		
		temp = imageS;
		imageS = altetiveImageS;
		altetiveImageS = temp;
	}
}
